package com.test.cordova.citylist.common;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.test.cordova.R;
import com.test.cordova.citylist.bean.City;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 城市列表
 */

public class CityAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {

    private List<City> citys ;
    private List<String> hotCitys ;
    private Context context;
    private int type = 0;
    private LayoutInflater inflater;

    public CityAdapter(List<City> citys, List<String> hotCitys, Context context) {
        this.citys = citys;
        this.hotCitys = hotCitys;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                View v =  inflater.inflate(R.layout.location_layout,parent,false);
                holder = new LocationCityViewHolder(v);
                break;
            case 1:
                View v1 =  inflater.inflate(R.layout.hot_city_layout,parent,false);
                holder = new HotCityViewHolder(v1);
                break;
            case 2:
                View v2 =  inflater.inflate(R.layout.city_list_item_layout,parent,false);
                holder = new CityViewHolder(v2);
                break;
            default:
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        Log.e("onBindViewHolder  ","LOCATION: "+(holder instanceof LocationCityViewHolder) + " Hot " + (holder instanceof HotCityViewHolder) +" City " +(holder instanceof CityViewHolder));

        if (holder instanceof LocationCityViewHolder) {
            LocationCityViewHolder locationCityViewHolder = (LocationCityViewHolder) holder;

        } else if (holder instanceof HotCityViewHolder){
            HotCityViewHolder hotCityViewHolder = (HotCityViewHolder) holder;
            hotCityViewHolder.hot_city_fl.setAdapter(new TagAdapter<String>(hotCitys)
            {
                @Override
                public View getView(FlowLayout parent, int position, String s)
                {
                    TextView tv = (TextView) inflater.inflate(R.layout.hot_city_item, hotCityViewHolder.hot_city_fl, false);
                    tv.setText(s);
                    return tv;
                }
            });

            hotCityViewHolder.hot_city_fl.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
            {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent)
                {
                    Toast.makeText(context, hotCitys.get(position), Toast.LENGTH_SHORT).show();
                    return true;
                }
            });

            hotCityViewHolder.hot_city_fl.setOnSelectListener(new TagFlowLayout.OnSelectListener()
            {
                @Override
                public void onSelected(Set<Integer> selectPosSet)
                {
                    Toast.makeText(context, selectPosSet.toString(), Toast.LENGTH_SHORT).show();
                }
            });

            //ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,hotCitys);
           /* SimpleAdapter adapter = new SimpleAdapter(context,hotCitys,R.layout.hot_city_item,new String[]{"name"},new int[]{R.id.hot_city_name});
            hotCityViewHolder.hot_city.setAdapter(adapter);
            hotCityViewHolder.hot_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(context,hotCitys.get(position).get("name"),Toast.LENGTH_SHORT).show();
                }
            });*/

        } else if (holder instanceof CityViewHolder) {
            int index = position - 2;
            CityViewHolder cityViewHolder = (CityViewHolder) holder;
            City cityEntity = citys.get(index);
            cityViewHolder.city_item_letter.setVisibility(View.VISIBLE);
            cityViewHolder.city_item_letter.setText(cityEntity.getFirsttPinyin());
            cityViewHolder.city_item_name.setText(cityEntity.getCityName());

            if (index >= 1) {
                City preCity = citys.get(index - 1);
                if (preCity.getFirsttPinyin().equals(cityEntity.getFirsttPinyin())) {
                    cityViewHolder.city_item_letter.setVisibility(View.GONE);
                } else {
                    cityViewHolder.city_item_letter.setVisibility(View.VISIBLE);
                }
            }
            cityViewHolder.city_item_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,cityEntity.getCityName(),Toast.LENGTH_SHORT).show();
                }
            });

        }

    }

    @Override
    public int getItemCount() {
        return citys.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                type = 0;
                break;
            case 1:
                type = 1;
                break;
            default:
                type = 2;
                break;
        }
        return type;
    }

    /**
     * 城市
     */
    class CityViewHolder extends RecyclerView.ViewHolder{

        private TextView city_item_letter;
        private TextView city_item_name;

        public CityViewHolder(View itemView) {
            super(itemView);
            city_item_letter = itemView.findViewById(R.id.city_item_letter);
            city_item_name = itemView.findViewById(R.id.city_item_name);
        }
    }

    /**
     * 热门城市
     */
    class HotCityViewHolder extends RecyclerView.ViewHolder{

        private GridView hot_city_gl;
        private TagFlowLayout hot_city_fl;

        public HotCityViewHolder(View itemView) {
            super(itemView);
            hot_city_gl = itemView.findViewById(R.id.hot_city_gl);
            hot_city_fl = itemView.findViewById(R.id.hot_city_fl);
        }
    }

    class LocationCityViewHolder extends RecyclerView.ViewHolder{

        public LocationCityViewHolder(View itemView) {
            super(itemView);
        }
    }



}
